package com.etlmaster.rowprocessor.core;

import java.util.HashMap;
import java.util.Map;

import com.etlmaster.executor.utils.JsonHelper;
import com.etlmaster.executor.utils.LogWriter;

/**
 * @author Larry
 * @copyright Larry@etlMaster
 * @since 2017-09-10
 */
public class JobManager {

	private JobManager() {

	}

	private static Map<String,Job> jobs=new HashMap<String,Job>();
	public static String startJob(Job job, Map<String, Object> jobParam) {

		try {
			job.initJob(jobParam);
			;
		} catch (EtlMasterRowProcessException e) {
			LogWriter.addLog("ERROR", "job {}初始化失败，请检查！{}", job.getId(),
					e.getMessage());
			e.printStackTrace();
			return job.getId() + "初始化失败，请检查，" + e.getMessage();
		} catch (Exception e) {
			LogWriter.addLog("ERROR", "job {}初始化失败，请检查！{}", job.getId(),
					e.getMessage());
			e.printStackTrace();
			return job.getId() + "初始化失败，请检查，" + e.getMessage();
		}
		getJobs().put(job.getId(), job);
		job.start();
		job.stop();
		return "ok";
	}



	public static String startJob(String jobjson) throws InterruptedException {
		Map<String, Object> jobParam = (Map<String, Object>) JsonHelper
				.getInstance().read(jobjson);
		String jobId = (String) jobParam.get("id");
		Job job  = new Job(jobId, jobjson);
		jobs.put(jobId, job);
		return startJob(job, jobParam);

	}



	public static Map<String,Job> getJobs() {
		return jobs;
	}

}
